package cn.dansj.enums;

import cn.dansj.common.utils.json.DictMap;
import cn.dansj.common.utils.transfer.ArrayUtils;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class GlobalEnv implements Serializable {
    public static final long SerialVersionUID = -999L;
    @Serial
    private static final long serialVersionUID = SerialVersionUID;

    /**
     * 用户基本信息关键字
     */
    public static final String SuperAdmin = "SuperAdmin";
    public static final String Admin = "admin";
    public static final String Normal = "normal";

    /**
     * Token默认有效期
     */
    public static final int TokenExpireDays = 1;

    /**
     * 用户一定时间内未操作时间token即过期，默认值
     */
    public static final int TokenNotOperatedMinutes = 30;

    /**
     * 加密token的有效期
     */
    public static final int EncryptTokenExpireSeconds = 5;

    /**
     * cacheToken前缀，从redis中获取token信息
     */
    public static final String CacheTokenPrefix = "Token@";

    /**
     * 二维码扫码登录前缀
     */
    public static final String QRCodePrefix = "QrCode@";

    /**
     * 全局线程池
     */
    public static final ExecutorService threadPool = Executors.newCachedThreadPool();

    /**
     * 企业微信相关配置信息
     */
    public static final String WeComPubMsgToken = "WeComToken@%s";
    public static final String WeComTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
    public static final String WeComMsgUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s";
    public static final String WeComUploadUrl = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=file";

    /**
     * 接口相关关键字以及默认token
     */
    public static final String WithProjectToken = "withProjectToken";
    public static final String InterfaceTotalCountPrefix = "interfaceTotalCount";
    public static final String ProjectDefaultBaseToken = "992545336541159989877455521";

    /**
     * redis lua脚本，接口限流
     */
    public static final String RedisLimitScript = """
            local key = KEYS[1]
            local time = tonumber(ARGV[1])
            local count = tonumber(ARGV[2])
            local current = redis.call('get', key)
            if current and tonumber(current) > count then
                return tonumber(current)
            end
            current = redis.call('incr', key)
            if tonumber(current) == 1 then
                redis.call('expire', key, time)
            end
            return tonumber(current)""";

    /**
     * redis lua脚本，token一天访问量统计
     */
    public static final String RedisTokenCountScript = """
            local key = KEYS[1]
            local expire = tonumber(ARGV[2])
            local operation = ARGV[1]
            local count = 0
            if operation == 'get' then
                count = redis.call('get', key)
            end
            if operation == 'incr' then
                count = redis.call('incr', key)
                if count ~= nil and expire ~= nil and tonumber(count) == 1 and expire > 0 then
                    redis.call('expire', key, expire)
                end
            end
            return tonumber(count)""";

    /**
     * minio上传信息key
     */
    public static final String MinioKeyPrefix = "minioUploadInfo@";

    /**
     * 异常code映射
     */
    public static final DictMap ErrorCodeMapping = DictMap.of("401", "无访问权限", "404", "PageNotFound", "500", "System Error");

    /**
     * 短链接域名
     */
    public static final String ShortLinkDomain = "https://x.dansj.cn/";

    /**
     * ordered 优先级
     */
    public static final int OrderIndexLogRecord = 10;

    /**
     * filter过滤器以及拦截器排除规则
     */
    public static final List<String> excludePathPatterns = ArrayUtils.asList("*.html", "*.js", "*.css", "*.woff", "*.ttf", "/error", "/favicon.*", "/xxlApi/**");

    /**
     * nexus host
     */
    public static final String NexusHost = "https://mvn.dansj.cn/webpage";

    /**
     * pierced nat host
     */
    public static final String NatHost = "http://dmp.dansj.cn:8000/api/";

    public static final String AesKey = "@!o#$%2*7%&5(0!9";
}
